package se.topshelf.golftracker.web.controller.player;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import se.topshelf.golftracker.core.domain.Player;
import se.topshelf.golftracker.core.service.BaseService;
import se.topshelf.golftracker.core.service.PlayerService;
import se.topshelf.golftracker.web.validator.BaseEntityValidator;

import java.text.MessageFormat;

/**
 *  Validates Player entities, so that they are ok save.
 */
public class PlayerValidator extends BaseEntityValidator<Player> {

    static final String FIRST_NAME_FIELD_NAME = "firstName";
    static final String LAST_NAME_FIELD_NAME = "lastName";
    static final String HANDICAP_FIELD_NAME = "handicap";
    static final String GOLF_ID_FIELD_NAME = "golfId";
    static final String GOLD_ID_DUPLICATE_DEFAULT_ERROR_MESSAGE = "Golf Id {0} is already assigned to other golfer.";
    static final String DUPLICATE_GOLF_ID_ERROR_CODE = "duplicateGolfId";
    private PlayerService playerService;


    public PlayerValidator(PlayerService playerService) {
        super(Player.class);
        this.playerService = playerService;
    }


    @Override
    protected void validateFields(Player entityToCheck, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, GOLF_ID_FIELD_NAME, REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIRST_NAME_FIELD_NAME, REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, LAST_NAME_FIELD_NAME, REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, HANDICAP_FIELD_NAME, REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "memberClub", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "homeAddress.street", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "homeAddress.streetNumber", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "homeAddress.postalCode", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "homeAddress.postalAddress", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "homeAddress.country", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
    }

    @Override
    protected BaseService getBaseService() {
        return playerService;
    }

    @Override
    protected boolean isNaturalKeyEqual(Player savedEntity, Player entityToCheck) {
        return savedEntity.getGolfIdAsString().equals(entityToCheck.getGolfIdAsString());
    }

    @Override
    protected void doRejectOfNaturalField(Errors errors, Player entityToCheck) {
        errors.rejectValue(GOLF_ID_FIELD_NAME, DUPLICATE_GOLF_ID_ERROR_CODE,
                MessageFormat.format(GOLD_ID_DUPLICATE_DEFAULT_ERROR_MESSAGE,
                        entityToCheck.getGolfIdAsString()));
    }
}
